import { resolve } from 'node:path'
import { defineConfig, splitVendorChunkPlugin } from 'vite'
import Vue from '@vitejs/plugin-vue'
import VueJsx from '@vitejs/plugin-vue-jsx'
import Pages from 'vite-plugin-pages'
import Layouts from 'vite-plugin-vue-layouts'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig(() => {
  return {
    base: '/work-time-demo/',

    build: {
      outDir: 'website',
    },

    server: {
      host: true,
      hmr: true,
      proxy: {
        // 本地接口代理
        '^/api/local': {
          rewrite: (path) => path.replace(/^\/api\/local\//, '/api'),
          target: '',
          changeOrigin: true,
        },
        '^/api': {
          target: '',
          changeOrigin: true,
        },
      },
    },

    plugins: [
      // https://github.com/vitejs/vite-plugin-vue
      Vue({
        script: {
          propsDestructure: true,
          defineModel: true,
        },
      }),
      VueJsx(),

      // https://github.com/hannoeru/vite-plugin-pages
      Pages({
        exclude: ['**/hooks/**/*', '**/utils/**/*', '**/constants/**/*', '**/components/**/*'],
        extensions: ['vue', 'ts', 'tsx'],
      }),

      // https://github.com/JohnCampionJr/vite-plugin-vue-layouts
      Layouts({
        exclude: ['**/components/**/*'],
      }),

      // https://github.com/antfu/unplugin-auto-import
      AutoImport({
        imports: [
          'vue',
          'vue-router',
          '@vueuse/head',
          '@vueuse/core',
          'pinia',
          {
            '@/http/request': ['useRequest'],
            '@neucloud/vue-app-sdk': ['useAppSDK', 'useRouteDetails'],
          },
        ],
        dts: 'src/types/auto-imports.d.ts',
        dirs: ['src/stores', 'src/hooks'],
        vueTemplate: true,
        resolvers: [ElementPlusResolver({ importStyle: false })],
      }),

      // https://cn.vitejs.dev/guide/build.html#chunking-strategy
      splitVendorChunkPlugin(),
    ],

    resolve: {
      alias: {
        '@/': `${resolve(__dirname, 'src')}/`,
        '~/': `${resolve(__dirname)}/`,
      },
    },
  }
})
